/*jslint browser: true*/
/*global $, jQuery, alert, window*/
function newTr(id, childList) {
    "use strict";
    var head = [],
        tail = [];
    head.push('<tr id="major_');
    head.push(id);
    head.push('">');
    tail.push('</tr>');
    return head.concat(childList, tail);
}
function newId(id) {
    "use strict";
    var aList = [];
    aList.push('<td id="id_');
    aList.push(id);
    aList.push('">');
    aList.push(id);
    aList.push('</td>');
    return aList;
}
function newName(id, name) {
    "use strict";
    var aList = [];
    aList.push('<td id="name_');
    aList.push(id);
    aList.push('">');
    aList.push(name);
    aList.push('</td>');
    return aList;
}
function newOp(id, url, name, className) {
    "use strict";
    var aList = [];
    aList.push('<td><a href="');
    aList.push(url || "#");
    aList.push('"><div class="');
    aList.push(className || "");
    aList.push('" id="del_');
    aList.push(id);
    aList.push('">');
    aList.push(name);
    aList.push('</div></a></td>');
    return aList;
}
function newUpdate(id, url) {
    "use strict";
    //return newOp(id, url, "修改名称", "update");
    return newOp(id, "#", "", "update");
}
function newDel(id, url) {
    "use strict";
    return newOp(id, url, "删除课程", "del_major");
}
function newCheck(id, url) {
    "use strict";
    return newOp(id, url, "查看章节");
}
//used in 生成页面
function addIntoTable(data) {
    "use strict";
    var index,
        length = data.length,
        item,
        aId,
        aName,
        aUpdate,
        aDel,
        aCheck,
        aTr;
    $("#major_list").hide();
    for (index = 0; index < length; index = index + 1) {
        item = data[index];
        aId = newId(item.id, item.name);
        aName = newName(item.id, item.name);
        aUpdate = newUpdate(item.id);
        aDel = newDel(item.id);
        aCheck = newCheck(item.id, "/manager/major/" + item.id + "/char/");
        aTr = newTr(item.id, aId.concat(aName, aUpdate, aDel, aCheck));
        $(aTr.join("")).appendTo("#major_list");
    }
    $("#major_list").show();
}

window.init = {
    "onload": function () {
        "use strict";
        //init body
        var major_all_key = "/major/",
            data;
        window.storageRemove(major_all_key);
        data = window.fetchData(window.storage("/major/"), window.format.major);
        window.storageRemove(major_all_key);
        addIntoTable(data);
        window.hideLoading();
        //删除课程,元素是之后创建的，延迟绑定
        $(".major_list").delegate("div.del_major", "click", function () {
            window.loading();
            var major_id = $(this).attr("id").split("_")[1],
                major_item_key = "/major/{major_id}/".format({major_id: major_id});
            $.ajax({
                url : major_item_key,
                type : 'DELETE',
                async : true,
                success : function (result, status) {
                    //window.deleteFromStorag("/major/", window.format.major, major_id);
                    //window.location.reload();
                    $("#major_" + major_id).detach();
                    window.hideLoading();
                }
            });
        });
        //新建课程
        $("#new_major_submit").click(function () {
            var data = JSON.stringify({
                    name : window.trim($("#new_major_name").val())
                });
            window.loading();
            $.ajax({
                url: major_all_key,
                type: "POST",
                dataType: "json",
                data: data,
                success: function (result, status) {
                    //var saved_major_list = window.storage("/major/"),
                    //    new_major_list = result.data;
                    //window.storage("/major/", saved_major_list.concat(new_major_list));
                    //window.location.reload();
                    addIntoTable(window.format.s2c(result.data, window.format.major));
                    window.hideLoading();
                }
            });
        });
    }
};
window.init_func_list = ["onload"];